#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/wait.h>

#define MAX_CMD 100
char command[MAX_CMD];

int main()
{
	while(1)
	{
	memset(command,'\0',MAX_CMD);
	printf("[晓林@myhost]$ ");
	fflush(stdout);
	fgets(command,MAX_CMD-1,stdin);//也可以使用gets(command);

	command[strlen(command)-1] = '\0';
	char* command_list[50];
	command_list[0] = strtok(command," ");
	int count = 0;
	while(command_list[count])
	{
		count++;
		command_list[count] = strtok(NULL," ");
	}
	if(strcmp(command_list[0],"cd") == 0 && chdir(command_list[1]) == 0)
		continue;
	pid_t pid = fork();
	if(pid == 0)
	{
		execvp(command_list[0],command_list);
	}
	int status;
	waitpid(pid,&status,0);
	if(WIFEXITED(status))
		printf("Exit Code: %d\n",(status>>8)&0xFF);
	else
		printf("Sig: %d\n",status&0x7F);
	}
	return 0;
}
